/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package org.amarena2d.examples.scenes.invader;

import com.android1.amarena2d.commons.event.Event;
import com.android1.amarena2d.commons.event.EventHandler;
import com.android1.amarena2d.engine.AmarenaEngine;
import com.android1.amarena2d.nodes.FpsLabel;
import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.nodes.layer.StaticBackground;
import com.android1.amarena2d.texture.Textures;
import com.badlogic.gdx.graphics.Color;

//todo work in progress.
public class InvaderExampleScene extends Scene {

    final PlayerShip ship;
    final InvaderBatch invader;
    final SpecialEffects specialEffects;

    final Shot playerShot;

    final Menu menu;

    public InvaderExampleScene() {


        StaticBackground background = new StaticBackground(Textures.$("invader/background.png").frame(0, 0, 480, 320).get());
        add(background);

        add(new FpsLabel(10, 20, Color.WHITE));

        add(invader = new InvaderBatch());

        add(ship = new PlayerShip());

        add(playerShot = new Shot());

        add(specialEffects = new SpecialEffects());

        add(menu = new Menu());

        engine.getEventBus().register(Events.START_GAME, new EventHandler() {
            @Override
            public void onEvent(Event event) {
                start();
            }
        });

        engine.getEventBus().register(Events.GAME_FINISHED, new EventHandler() {
            @Override
            public void onEvent(Event event) {
                finished();
            }
        });

    }

    private void start() {
         AmarenaEngine e = engine;
        invader.start();
        ship.reset();
        playerShot.start(ship);
        touch().makeTouchable(this);
    }

    private void finished() {
        touch().revokeTouchable();
        playerShot.stopFireing();
        ship.finished();
        invader.stop();
        menu.show();
    }


    @Override
    public boolean onTouchDragged(float x, float y, int pointer, boolean isHit) {
        ship.calcMove(x);
        return true;
    }

    @Override
    public boolean onTouchUp(float x, float y, int pointer, boolean isHit) {
        ship.stop();
        return true;
    }

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        ship.calcMove(x);
        return true;
    }

    @Override
    protected void onDispose() {

    }
}